
--------------------------------------------------------------------------------
-- 数据库名称       : RYAccountsDB
-- 数据对象名称     : dbo.CPP_BindSpreaderCode
-- DecryptSQL 版本  : Ver 3.2.0
-- DecryptSQL 网站  : http://www.devlib.net/cn/
--------------------------------------------------------------------------------

-- 更新资料
ALTER PROC [dbo].[CPP_BindSpreaderCode]
  @dwUserID INT,    -- 用户ID
  @dwCode INT,      -- 六位推广员GameID
  @dwKindID INT,
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS

-- 属性设置
SET NOCOUNT ON

-- 执行逻辑
BEGIN

  DECLARE @Channel INT = 0
  SELECT @Channel = Channel FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 1
  END

  -- 渠道号判断
  DECLARE @State INT = 0
  SELECT @State = State FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在'
    RETURN 2
  END

  IF @State<>1 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']未启用'
    RETURN 3
  END

  DECLARE @AUserID INT = 0
  SELECT @AUserID = UserID FROM AccountsInfo(NOLOCK) WHERE AccountsType = 1 AND GameID = @dwCode
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'邀请码[' + CAST(@dwCode AS NVARCHAR) + ']不存在'
    RETURN 4
  END

  -- 小游戏判断
  DECLARE @KindID INT = 0
  DECLARE @KindName NVARCHAR(255) = 'XX游戏'
  SELECT @KindID = ISNULL(KindID, 0), @KindName = KindName FROM RYPlatformDB.dbo.GameKindItem WHERE Nullity = 0 AND KindID = @dwKindID
  IF @@ROWCOUNT<>1 BEGIN
    SET @szResult = N'游戏[' + CAST(@dwKindID AS NVARCHAR) + ']未定义'
    RETURN 5
  END

  DECLARE @ID INT = 0
  DECLARE @SpreaderID INT = 0
  SELECT @ID = ID, @SpreaderID = SpreaderID FROM RYTreasureDB.dbo.UserRoomCard WHERE UserID = @dwUserID AND KindID = @dwKindID
  IF @@ROWCOUNT=0 BEGIN
    INSERT INTO [RYTreasureDB].[dbo].[UserRoomCard] (
      [UserID], [KindID], [SpreaderID], [SpreaderIDBindTime]
    ) VALUES (
      @dwUserID, @dwKindID, @AUserID, GETDATE()
    )
    SET @ID = @@IDENTITY
  END ELSE BEGIN
    IF @SpreaderID>0 BEGIN
      DECLARE @OldBindCode INT = 0
      SELECT @OldBindCode = GameID FROM AccountsInfo(NOLOCK) WHERE AccountsType = 1 AND UserID = @SpreaderID
      SET @szResult = N'您的帐号已经绑定了[' + @KindName + ']邀请码[' + CAST(@OldBindCode AS NVARCHAR) + '],不能重复绑定!'
      RETURN 6
    END ELSE BEGIN
      UPDATE [RYTreasureDB].[dbo].[UserRoomCard] SET SpreaderID = @AUserID, SpreaderIDBindTime = GETDATE() WHERE ID = @ID
    END
  END

  DECLARE @AutoFamily INT = 0
  SELECT @AutoFamily = AutoFamily FROM RYWebSMSDB.dbo.AgentReg WHERE GameID = @dwCode
  IF @AutoFamily = 1 BEGIN -- 开启自动同步家园
    EXEC RYWebSMSDB.dbo.PHP_bindFamily @dwUserID, @AUserID
  END

  DECLARE @hasZ INT = 0
  SELECT @hasZ = COUNT(*) FROM RYRecordDB.dbo.CostRoomCardRecord WHERE [dwCostType] = 9 AND [dwUserID] = @dwUserID -- AND KindID = @dwKindID
  IF @hasZ=0 BEGIN
    -- 获取绑定推广员赠送房卡数
    DECLARE @BindSpreaderFinsh AS INT = 0
    SELECT @BindSpreaderFinsh = ISNULL(ZBind, 0) FROM RYPlatformDB.dbo.GameChannelKind WHERE [State] = 1 AND Channel = @Channel AND KindID = @dwKindID
    IF @@ROWCOUNT=0 BEGIN
      SET @szResult = N'绑定邀请码成功, 获取赠送房卡数失败'
      RETURN 7
    END
    IF @BindSpreaderFinsh>0 BEGIN
      EXEC RYTreasureDB.dbo.CPP_User_RoomCard_Write @dwUserID, @BindSpreaderFinsh, 9, @dwKindID
    END
  END

  SET @szResult = N'绑定邀请码成功'

  RETURN 0
END
